package cn.wx.scholar.core.service.importNeo4j;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import com.csvreader.CsvReader;

import cn.wx.common.utils.JSONUtils;
public class QcReadCsv {

	
	public static void main(String[] args) {
		
		final String filePath = "D:\\my_read_csv\\ms_expert_paper.csv";
		final String filePathQc = "D:\\my_read_csv\\ms_expert_paper_qc.csv";
		try {
			
			File file = new File(filePath);
	        FileInputStream fileInputStream = new FileInputStream(file);  
	        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			
			CsvReader csvReader = new CsvReader(bufferedInputStream,Charset.forName("utf-8"));
			
			StringBuffer sb =null;
			BufferedWriter output = null;
			// 读表头
            csvReader.readHeaders();
 
            int i = 0;
            
            Map<String,String> qcMap = new HashMap<>();
            
            while (csvReader.readRecord()){
            	
            	//获取fid，微软唯一序列号
            	String fId = csvReader.get(0);
                // 读一整行
            	String line = csvReader.getRawRecord();
				System.out.println("读取" + (i++) +"行");
				qcMap.put(fId, line);
            }
			
            File of = new File(filePathQc);
			FileOutputStream ofs = new FileOutputStream(of,true);
			OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
			output = new BufferedWriter(ofw);
			
			sb = new StringBuffer();
			sb.append(JSONUtils.c("expertId")).append(",").append(JSONUtils.c("orgId"));
			
			output.write(sb.toString());
			output.newLine();
            
			for(String key:qcMap.keySet()){
				String value = qcMap.get(key);
				output.write(value);
				output.newLine();
				output.flush();
			}

            output.flush();
            output.close();
            output = null;
            
            /**
			 * 关闭读入流
			 */
			csvReader.close();
			csvReader = null;
            
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
